package com.lsmo.android.carfans.activity.car;

import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.app.widget.RemoteImageView;
import com.lsmo.android.carfans.model.car.Brand;
import com.lsmo.android.util.ArrayListAdapter;

public class BrandAdapter extends ArrayListAdapter<Brand>{
	public static final String TAG = "BrandAdapter";
	
	public BrandAdapter(Context context) {
		super(context);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View localView1 = convertView;
	    View localView2;
		LayoutInflater mInflater = ((Activity) mContext).getLayoutInflater();
		ViewHolder localViewHolder;
		InputStream localInputStream;
		//获取list数据
		//Map mapinfo = (Map) mList.get(position);
		
		if(localView1 == null ){
			localView1 = mInflater.inflate(R.layout.car_adapter_brand, null);
			
			localViewHolder = new ViewHolder();
			localViewHolder.textView = (TextView)localView1.findViewById(R.id.brandname);//文字
			localViewHolder.imageView = (RemoteImageView)localView1.findViewById(R.id.carbrandimage);//图标
			
			localView1.setTag(localViewHolder);
		} else {
			localViewHolder = (ViewHolder) localView1.getTag();
		}
		try {
		    //图标
	    	AssetManager localAssetManager = this.mContext.getResources().getAssets();
	    	String str1 = ((Brand)mList.get(position)).getImage();
//	    	String str1 = (String) mapinfo.get("Image");
			localInputStream = localAssetManager.open(str1);
			
			RemoteImageView localRemoteImageView2 = localViewHolder.imageView;
		    String str2 = ((Brand)mList.get(position)).getImage();
		    Drawable localDrawable = Drawable.createFromStream(localInputStream, str2);
		    localRemoteImageView2.setBackgroundDrawable(localDrawable);
		    
		    //汽车名称name
		    String str3 =  ((Brand)mList.get(position)).getName();
		    localViewHolder.textView.setText(str3);
		    
	        //设置默认的图片背景
//		    Integer localInteger = Integer.valueOf(R.drawable.default_image);
//	        localViewHolder.imageView.setDefaultImage(localInteger);
//	        String strImg = ((Brand)mList.get(position)).getImage();
//	        ListView localListView = getListView();
//	        localViewHolder.imageView.setImageUrl(strImg, position, localListView);
		    
			//大标题例如A(奥迪)、B（宝马）
//		    localView2 = mInflater.inflate(R.layout.adapter_brand_title, null);
//		    localViewHolder = new ViewHolder();
//		    localViewHolder.textView = (TextView)localView2.findViewById(R.id.title_group);
//		    localView2.setTag(localViewHolder);
//			String str = ((Brand)mList.get(position)).getFirstLetter();
//		    localViewHolder.textView.setText(str.toUpperCase());
//		    return localView2;
		    
		} catch (Exception e) {
			e.printStackTrace();
		}
	    return localView1;
	}

	static class ViewHolder
	{
	    RemoteImageView imageView;
	    TextView textView;
	}

}
